package com.wisdom.weld.common;

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public final class ConfigurationFactory {

    private static Configuration cfg;

    private ConfigurationFactory() {
    }

    public static Configuration getConfiguration(String templateFilePath){
        if (cfg != null) {
            return cfg;
        }
        try{
            // 创建FreeMarker
            cfg = new Configuration(Configuration.VERSION_2_3_22);
            // 设置模板加载目录
//        cfg.setClassForTemplateLoading(App.class, "resources/template");
            cfg.setDirectoryForTemplateLoading(new File(templateFilePath));
            // 设置默认编码
            cfg.setDefaultEncoding("UTF-8");
            //
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            return cfg;
        }catch(IOException e){
            throw new RuntimeException("创建FreeMarker Configuration异常");
        }
    }

    public static Configuration getConfiguration(){
        URL resource = ClassLoader.getSystemResource("template");
        return getConfiguration(resource.getPath());
    }


}
